package br.com.fogas.database.entidade;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import br.com.fogas.database.UtilsDatabase;
import br.com.fogas.database.IMetadata;
import br.com.fogas.utils.StringUtility;
import br.com.fogas.webservice.parser.material.MaterialResponseItem;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName=IMetadata.IMaterial.table_name)
public class Material implements IMetadata, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@DatabaseField(generatedId = true, columnName=IMaterial.id)
	private Integer id;
	
	@DatabaseField(unique=true, canBeNull=false, width=18, columnName=IMaterial.tx_cod_material)
	private String tx_cod_material;
	
	@DatabaseField(columnName=IMaterial.tx_desc_mat)
	private String tx_desc_mat;
	
	@DatabaseField(columnName=IMaterial.tx_und)
	private String tx_und;
	
	@DatabaseField(columnName=IMaterial.tx_cod_cliente)
	private String tx_cod_cliente;
	
	@DatabaseField(columnName=IMaterial.tx_centro)
	private String tx_centro;
	
	@DatabaseField(dataType = DataType.BIG_DECIMAL_NUMERIC, columnName=IMaterial.vl_pmpf)
	private BigDecimal vl_pmpf;
	
	@DatabaseField(dataType = DataType.BIG_DECIMAL_NUMERIC, columnName=IMaterial.vl_aliquota)
	private BigDecimal vl_aliquota;
	
	@DatabaseField(columnName=IMaterial.dt_vigencia)
	private Date dt_vigencia;
	
	public Material() {
		// TODO Auto-generated constructor stub
	}
	
	public Material(MaterialResponseItem item) {
		this.tx_cod_cliente = item.getKUNNR();
		this.tx_centro = item.getWERKS();
		this.tx_cod_material = item.getMATNR();
		this.tx_desc_mat = item.getZMENS();
		this.tx_und = item.getUnidade();
		this.vl_pmpf = getValorPmpf(item.getZVALI());
		this.vl_aliquota = getValorAliquota(item.getZVALIC());
		this.dt_vigencia = UtilsDatabase.getDataDefault(item.getZDATE());
	}
	
	private BigDecimal getValorAliquota(String value) {
		if(StringUtility.isNotEmpty(value)) {
			try {
				Double valor = (Double.valueOf(value) / 100);
				return BigDecimal.valueOf(valor).setScale(2, BigDecimal.ROUND_HALF_UP);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	private BigDecimal getValorPmpf(String value) {
		if(StringUtility.isNotEmpty(value)) {
			try {
				Double valor = Double.valueOf(value);
				return BigDecimal.valueOf(valor).setScale(2, BigDecimal.ROUND_HALF_UP);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTx_cod_material() {
		return tx_cod_material;
	}

	public void setTx_cod_material(String tx_cod_material) {
		this.tx_cod_material = tx_cod_material;
	}

	public String getTx_desc_mat() {
		return tx_desc_mat;
	}

	public void setTx_desc_mat(String tx_desc_mat) {
		this.tx_desc_mat = tx_desc_mat;
	}

	public String getTx_und() {
		return tx_und;
	}

	public void setTx_und(String tx_und) {
		this.tx_und = tx_und;
	}

	public BigDecimal getVl_pmpf() {
		return vl_pmpf;
	}

	public void setVl_pmpf(BigDecimal vl_pmpf) {
		this.vl_pmpf = vl_pmpf;
	}

	public BigDecimal getVl_aliquota() {
		return vl_aliquota;
	}

	public void setVl_aliquota(BigDecimal vl_aliquota) {
		this.vl_aliquota = vl_aliquota;
	}

	public Date getDt_vigencia() {
		return dt_vigencia;
	}

	public void setDt_vigencia(Date dt_vigencia) {
		this.dt_vigencia = dt_vigencia;
	}

	public String getTx_cod_cliente() {
		return tx_cod_cliente;
	}
	
	public void setTx_cod_cliente(String tx_cod_cliente) {
		this.tx_cod_cliente = tx_cod_cliente;
	}

	public String getTx_centro() {
		return tx_centro;
	}

	public void setTx_centro(String tx_centro) {
		this.tx_centro = tx_centro;
	}
}